package com.lsmw.management.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsmw.commonutils.R;
import com.lsmw.management.entity.LsHotelInfo;
import com.lsmw.management.service.LsHotelInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 所有酒店基本信息 前端控制器
 * </p>
 *
 * @author 二号男嘉宾
 * @since 2020-12-02
 */
@CrossOrigin
@Api(tags = "酒店信息")
@RestController
@RequestMapping("/management/hotel")
public class LsHotelInfoController {

    private LsHotelInfoService lsHotelInfoService;

    @Autowired
    public LsHotelInfoController(LsHotelInfoService lsHotelInfoService) {
        this.lsHotelInfoService = lsHotelInfoService;
    }

    @ApiOperation(value = "获取所有酒店列表")
    @GetMapping("/")
    public R getHotelList() {
        return lsHotelInfoService.getHotelList();
    }

    @ApiOperation(value = "获取所有酒店名")
    @GetMapping("/hotelNames/{str}")
    public R getHotelNames(
            @ApiParam(name = "str", value = "查询字符串", required = true)
            @PathVariable String str) {
        if (str.equals("null")) {
            return R.ok().data("rows",
                    lsHotelInfoService.list(new LambdaQueryWrapper<LsHotelInfo>().select(LsHotelInfo::getName)));
        }
        return R.ok().data("rows",
                lsHotelInfoService.list(new LambdaQueryWrapper<LsHotelInfo>()
                        .select(LsHotelInfo::getName)
                        .like(LsHotelInfo::getName, str).last("limit 0, 17")));
    }

    @ApiOperation(value = "根据ID获取酒店信息")
    @GetMapping("/info/{id}")
    public R getHotelById(
            @ApiParam(name = "id", value = "酒店id", required = true)
            @PathVariable String id) {
        return R.ok().data("row", lsHotelInfoService.getById(id));
    }

    @ApiOperation(value = "添加酒店")
    @PostMapping("/")
    public R addHotel(
            @ApiParam(name = "lsHotelInfo", value = "酒店数据(json)", required = true)
            @RequestBody LsHotelInfo lsHotelInfo) {
        return lsHotelInfoService.addHotel(lsHotelInfo);
    }

    @ApiOperation(value = "根据ID修改酒店信息")
    @PutMapping("/{id}")
    public R updateHotelById(
            @ApiParam(name = "id", value = "酒店id", required = true)
            @PathVariable String id,
            @ApiParam(name = "lsHotelInfo", value = "酒店数据(json)", required = true)
            @RequestBody LsHotelInfo lsHotelInfo) {
        return lsHotelInfoService.updateHotelById(id, lsHotelInfo);
    }

    @ApiOperation(value = "根据ID删除酒店")
    @DeleteMapping("/{id}")
    public R removeHotelById(
            @ApiParam(name = "id", value = "酒店id", required = true)
            @PathVariable String id) {
        return lsHotelInfoService.removeHotelById(id);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pageHotel/{current}/{limit}")
    public R getHotelListPage(
            @ApiParam(name = "current", value = "当前页下标", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        Page<LsHotelInfo> hotelPage = new Page<>(current, limit);
        lsHotelInfoService.page(hotelPage, null);
        Map map = new HashMap();
        map.put("total", hotelPage.getTotal());
        map.put("rows", hotelPage.getRecords());
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据name查询酒店信息")
    @GetMapping("/{name}/{current}/{limit}")
    public R getHotelInfoById(
            @ApiParam(name = "name", value = "酒店name", required = true) @PathVariable String name,
            @ApiParam(name = "current", value = "当前页下标", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        Page<LsHotelInfo> hotelPage = new Page<>(current, limit);
        if (name == Character.toString('0')) {
            lsHotelInfoService.page(hotelPage, null);
        } else {
            lsHotelInfoService.page(hotelPage, new LambdaQueryWrapper<LsHotelInfo>().like(LsHotelInfo::getName, name));
        }
        return R.ok().data("rows", hotelPage.getRecords()).data("total", hotelPage.getTotal());
    }

}

